home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Original Shareware 1.1
/
The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso
/
23
/
lazer12.zip
/
LAZER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-10-31
|
13KB
|
762 lines
/********************************************************************
* LAZER.C Version 1.2 Copyright (c) 1987 Jim Bumgardner
*
* Text Formatter
*
* for OKIDATA LASERLINE (HP Compatable) Laser Printer
*
* LAZER has been compiled in Manx (Aztec) C, Turbo C and VAX C.
*
********************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <math.h>
FILE *ifile,*ofile;
int lineno,pageno,copies;
char author[] = "(c) James Bumgardner 1987";
char *invcmd = "Invalid Command";
char *invarg = "Invalid Argument";
char *misarg = "Missing Argument";
char *premeof = "Premature End of File";
char *strchr(),*get_filename();
void *malloc();
int grey = 25,pattern = 1,port = 0;
char outfile = 0,ignore_cr = 0;
char nodownload;
#define pputc(c,p) biosprint(0,c,p)
#define init_printer(p) biosprint(1,0,p)
unsigned char cmdflag;
main(argc,argv)
char *argv[];
{
int c;
parse_cmd(argc,argv);
cmdflag = 0;
while (1)
{
c = getc2();
if (c == EOF) {
if (close_file())
break;
else {
cmdflag = 1;
continue;
}
}
if (cmdflag)
{
switch (c) {
case '[': outchar('['); /* no break */
case ']': cmdflag = 0; break;
case ';': skip_comment(); break;
case '\n': break;
case 'F': get_font(); break;
case 'M': get_margins(); break;
case 'I': if (include_file()) cmdflag = 0;
break;
case 'T':
case 'P':
case 'p':
case 'G':
case 'H':
case 'V':
case 'D':
case 'B':
case 'A':
case 'O':
get_nval(c); break;
case '/':
case '!':
case '_': toggle_effect(c); break;
case 'f': if (copies > 1)
pprintf("\033&l%dX",copies);
outchar(12); break;
case '(': symbol_set(); break;
case 'L': locate_cursor(); break;
case 'R': print_rule(); break;
case 'E': outchar(27); break;
case '*': macro_handler(); break;
case '^': get_cntrl(); break;
case 'N': toggle_flag(&ignore_cr); break;
}
}
else {
if (c == '[')
cmdflag = 1;
else {
if (c == '\n') {
if (ignore_cr) continue;
outchar('\r');
}
outchar(c);
}
}
}
if (copies > 1)
pprintf("\033&l%dX",copies);
outstr("\033E");
}
parse_cmd(argc,argv)
char *argv[];
{
char *ifname,*ofname;
int i,synflg;
synflg = 0;
ifname = ofname = NULL;
for (i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch(toupper(argv[i][1])) {
case 'N': nodownload = 1;
break;
case 'C': copies = atoi(&argv[i][2]);
break;
default: synflg = 1; break;
}
}
else {
if (ifname) ofname = argv[i];
else ifname = argv[i];
}
}
if (ifname == 0 || synflg)
{
fputs(
"Syntax:\n\tLAZER [options] file [port# or file]\n"
"\nOptions:\n"
"\t-N\tDon't Download Fonts\n"
"\t-C#\tCopies\n"
"\nExamples:\n"
"\tLAZER TEST.TXT 2\n"
"\tLAZER -C3 TEST.TXT TEXT.OUT\n",stderr);
exit();
}
if (ofname) {
str_toupper(ofname);
if (strcmp(ofname,"2") == 0 ||
strcmp(ofname,"LPT2") == 0 ||
strcmp(ofname,"LPT2:") == 0)
port = 1;
else if (strcmp(ofname,"1") &&
strcmp(ofname,"LPT1") &&
strcmp(ofname,"LPT1:"))
{
outfile = 1;
if ((ofile = fopen(ofname,"wb")) == 0)
{
fprintf(stderr,"Can't open %s for output\n",ofname);
exit();
}
}
}
if (ofile == 0) init_printer(port);
open_file(ifname,".TXT");
}
str_toupper(str)
char *str;
{
while (*str) *str++ = toupper(*str);
}
skip_comment()
{
register int c;
do {
c = getc2();
} while (c != '\n' && c != EOF);
}
get_number()
{
register int c;
char pts[8],*p;
p = pts;
c = getc2();
while (isdigit(c) || c == '.' || c == '-')
{
*p++ = c;
c = getc2();
}
ungetc2(c);
*p = 0;
return(atoi(pts));
}
char efstr[] = "!/_";
char *efcmd[3][2] =
{"\033(s0B","\033(s3B", /* Bold */
"\033(s0S","\033(s1S", /* Italic */
"\033&d@","\033&dD"}; /* Underlining */
char eftog[3];
toggle_effect(c)
int c;
{
char *p;
int effect;
c = toupper(c);
p = strchr(efstr,c); /* Already been checked */
effect = (int) p - (int) efstr;
eftog[effect] ^= 1;
outstr(efcmd[effect][eftog[effect]]);
if (effect == 1 && eftog[effect] == 0 && eftog[0])
outstr(efcmd[0][1]);
if (effect == 0 && eftog[effect] == 0 && eftog[1])
outstr(efcmd[1][1]);
}
get_font()
{
int fno;
if (is_number())
{
fno = get_number();
if (next_char() == '<') download_font(fno);
pprintf("\033(%dX",fno);
}
else
print_err(invarg);
}
download_font(fno)
{
char *p;
int c;
p = get_filename();
if (p && nodownload == 0) {
pprintf("\033*c%dD",fno);
open_file(p,".SFP");
fprintf(stderr,"<%s\n",p);
while ((c = getc(ifile)) != EOF)
outchar(c);
close_file();
pprintf("\033*c%dd5F",fno);
}
}
get_cntrl()
{
int c;
c = getc2();
c = toupper(c);
outchar(c - '@');
}
symbol_set()
{
int c;
outchar(27); outchar('(');
c = getc2(); outchar(c);
c = getc2(); outchar(c);
}
char marstr[] = {"LRTC"};
char *marcmd[] =
{"\033&a%dL",
"\033&a%dM",
"\033&l%dE"};
get_margins()
{
char *p;
int c,mar,val;
c = getc2();
c = toupper(c);
if ((p = strchr(marstr,c)) == NULL)
{
print_err(invarg);
return;
}
mar = (int) p - (int) marstr;
if (mar == 3) {
outstr("\0339");
return;
}
else {
if (is_number() == 0)
{
print_err(misarg);
return;
}
val = get_number();
pprintf(marcmd[mar],val);
}
}
get_nval(c)
{
int val;
if (is_number() == 0)
return(print_err(misarg));
val = get_number();
switch(c) {
case 'T':
case 'V':
case 'H':
case 'B':
case 'P': pprintf("\033(s%d%c",val,c); break;
case 'O':
case 'D': pprintf("\033&l%d%c",val,c); break;
case 'A': pprintf("\033*c%dd6F",val); break;
case 'p': pattern = val; break;
case 'G': grey = val; break;
default: print_err(invcmd); break;
}
}
char *signstr[] = {"-","","+"};
locate_cursor()
{
int h,v;
int c,i,hs,vs;
i = next_char();
hs = vs = 0;
if (i == ',' || i == '+' || i == '-' || isdigit(i))
{
if (i == '+' || i == '-') {
if (i == '+') hs = 1;
else hs = -1;
getc2();
i = next_char();
}
if (isdigit(i)) {
h = get_number();
i = next_char();
}
else h = -1;
if (i == ',')
{
getc2();
i = next_char();
if (i == '+' || i == '-') {
if (i == '+') vs = 1;
else vs = -1;
getc2();
}
v = get_number();
}
else v = -1;
if (h >= 0) pprintf("\033&a%s%dH",signstr[hs+1],h);
if (v >= 0) pprintf("\033&a%s%dV",signstr[vs+1],v);
}
else {
c = getc2();
switch (c) {
case 'S': outstr("\033&f0S"); break;
case 'R': outstr("\033&f1S"); break;
default: return(print_err(invcmd));
}
}
}
macro_handler()
{
int val;
int c;
c = next_char();
if (isdigit(c))
{
val = get_number();
pprintf("\033&f%dy2X",val);
}
else {
getc2();
switch (c) {
case '{': val = get_number();
pprintf("\033&f%dy0X",val);
break;
case '}': outstr("\033&f1X");
break;
default: print_err(invcmd);
}
}
}
print_rule()
{
int h,v;
int c;
c = getc2();
h = get_number();
getc2();
v = get_number();
switch(toupper(c)) {
case 'S':
case 'B': pprintf("\033*c%dh%dv0P",h,v); break;
case 'G': pprintf("\033*c%dg%dh%dv2P",grey,h,v); break;
case 'P': pprintf("\033*c%dg%dh%dv3P",pattern,h,v); break;
}
}
toggle_flag(f)
char *f;
{
int c;
c = getc2();
switch (toupper(c)) {
case '+':
case 'Y': *f = 1; break;
case '-':
case 'N': *f = 0; break;
default: *f ^= 1;
ungetc2(c);
break;
}
}
char *get_filename()
{
static char fname[81];
char *p;
int c;
c = getc2();
if (c != '<') {
print_err(invcmd);
return(NULL);
}
p = fname;
while (1) {
c = getc2();
if (c == '>' || c == EOF)
break;
*p++ = c;
}
if (c == EOF) {
print_err(premeof);
return(NULL);
}
*p = '\0';
return(fname);
}
include_file()
{
char *p;
int c;
c = next_char();
if (toupper(c) == 'B')
get_bmap();
else if (toupper(c) == 'P')
get_pcx();
else if (toupper(c) == 'R')
get_raw();
else {
p = get_filename();
if (p) {
open_file(p,".TXT");
return(1);
}
}
return(0);
}
get_raw()
{
char *p;
int c;
getc2();
p = get_filename();
if (p) {
open_file(p,".PRN");
fprintf(stderr,"<%s(R)\n",p);
while ((c = getc(ifile)) != EOF)
outchar(c);
close_file();
}
}
get_bmap()
{
int reso,ydim,xdim;
int y,c,invert;
char *buf,*p,bl;
reso = 300;
getc2();
c = next_char();
if (toupper(c) == 'I') {
invert = 1;
getc2();
}
else invert = 0;
if (is_number())
reso = get_number();
p = get_filename();
if (p) {
open_file(p,".MAP");
xdim = getw(ifile);
ydim = getw(ifile);
fprintf(stderr,"%s: %d X %d\n",p,xdim,ydim);
xdim >>= 3;
buf = malloc(xdim+16);
sprintf(buf,"\033*b%dW",xdim);
bl = strlen(buf);
pprintf("\033*t%dR\033*r1A",reso);
y = 0;
while (y++ < ydim && fread(buf+bl,xdim,1,ifile))
{
if (invert) invert_bmap(buf+bl,xdim);
outbuf(buf,xdim+bl);
if ((y & 63) == 0)
fprintf(stderr,"\r%d/%d",y - 1,ydim);
}
fprintf(stderr,"\n");
outstr("\033*rB");
close_file();
free(buf);
}
}
get_pcx()
{
int reso,ydim,xdim;
int y,x,c,n,invert;
char *buf,*bufptr,*p,bl;
int pcx_hed[64];
reso = 300;
getc2();
c = next_char();
if (toupper(c) == 'I') {
invert = 1;
getc2();
}
else invert = 0;
if (is_number())
reso = get_number();
p = get_filename();
if (p == NULL) return;
open_file(p,".PCX");
fread(pcx_hed,0x80,1,ifile);
xdim = pcx_hed[4]+1;
ydim = pcx_hed[5]+1;
fprintf(stderr,"%s: %d X %d\n",p,xdim,ydim);
xdim = pcx_hed[33];
buf = malloc(xdim+16);
sprintf(buf,"\033*b%dW",xdim);
bl = strlen(buf);
bufptr = buf+bl;
pprintf("\033*t%dR\033*r1A",reso);
for (y = 0; y < ydim; ++y)
{
x = 0;
while (x < xdim) {
if ((c = getc(ifile)) == EOF)
{
fprintf(stderr,"\nPremature EOF\n");
break;
}
if ((c & 0xC0) != 0xC0)
bufptr[x++] = c;
else {
n = c & 0x3F;
c = getc(ifile);
while (n--)
bufptr[x++] = c;
}
}
if (c == EOF) break;
if (invert) invert_bmap(bufptr,xdim);
outbuf(buf,xdim+bl);
if ((y & 63) == 0)
fprintf(stderr,"\r%d/%d",y,ydim);
}
fprintf(stderr,"\n");
outstr("\033*rB");
close_file();
free(buf);
}
invert_bmap(str,n)
unsigned char *str;
{
while (n--)
{
*str++ = ~*str;
}
}
/************************************
* File Handling
*
************************************/
FILE *save_file[16];
int filecount;
int linecount[16];
char *sfname[16];
open_file(fname,ext)
char *fname,*ext;
{
if (filecount == 16)
return(print_err("Too Much Include Nesting"));
linecount[filecount] = lineno;
lineno = linecount[filecount] = 0;
sfname[filecount] = malloc(strlen(fname) + 5);
strcpy(sfname[filecount],fname);
if (strchr(fname,'.') == NULL) strcat(sfname[filecount],ext);
if ((ifile = fopen(sfname[filecount],"rb")) == NULL)
{
fprintf(stderr,"Can't open %s",sfname[filecount]);
exit();
}
save_file[filecount] = ifile;
++filecount;
}
close_file()
{
fclose(ifile);
--filecount;
free(sfname[filecount]);
if (filecount)
{
lineno = linecount[filecount-1];
ifile = save_file[filecount-1];
return(0);
}
else return(1);
}
/*****************
* Look Ahead
*****************/
next_char()
{
register int c;
c = getc2();
ungetc2(c);
return(c);
}
is_number()
{
register int c;
c = next_char();
return(isdigit(c));
}
/********************
* Character I/O
********************/
char unget;
savebuf[257];
ungetc2(c) /* Supports 256 ungets (First in, Last Out) */
{
savebuf[unget++] = c;
}
getc2()
{
register int c;
if (unget) c = savebuf[--unget];
else c = getc(ifile);
if (c == 26) return(EOF);
if (c == '\r') {
c = getc(ifile);
if (c != '\n') {
ungetc(c,ifile);
return('\r');
}
else ++lineno;
}
return(c);
}
/***********************
* Error Handling
***********************/
print_err(str)
char *str;
{
if (filecount > 1)
fprintf(stderr,"File %s - Line %d: %s\n",
sfname[filecount-1],lineno+1,str);
else
fprintf(stderr,"Line %d: %s\n",
lineno+1,str);
return(0);
}
/**********************
* Printer Output
**********************/
outbuf(buf,len)
char *buf;
{
if (outfile)
fwrite(buf,len,1,ofile);
else
while (len--)
pputc(*buf++,port);
}
pprintf(tmp,a1,a2,a3)
char *tmp;
{
char obuf[81];
sprintf(obuf,tmp,a1,a2,a3);
outstr(obuf);
}
outstr(buf)
char *buf;
{
if (outfile)
while (*buf)
putc(*buf++,ofile);
else
while (*buf)
pputc(*buf++,port);
}
outchar(c)
{
static int tx;
static char tabs[] = " ";
if (cmdflag == 0) { /* Deal with Tabs Correctly */
switch (c) {
case '\r':
case '\n': tx = 0; break;
case '\t': outbuf(tabs,8 - tx%8);
tx += 8-tx%8;
return;
case '\b': --tx; break;
default: ++tx; break;
}
}
if (outfile)
putc(c,ofile);
else
pputc(c,port);
}